package com.xuef.ibatis.binding;

import com.xuef.ibatis.session.Configuration;
import com.xuef.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by xuefengDong: moveb
 * On 2019/4/26- 16:07
 * 注册Mapper接口与获取生成代理类实例的工具类
 */
public class MapperRegistry {
    //全局配置文件对象
    private Configuration config;
    //HashMap Key是mapper的类型对象, Value是MapperProxyFactory对象
    //这个MapperProxyFactory是创建Mapper代理对象的工厂
    private final Map<Class<?>, MapperProxyFactory<?>> knownMappers =
            new HashMap<Class<?>, MapperProxyFactory<?>>();

    public MapperRegistry(Configuration config) {
        this.config = config;
    }
    //获取生成的代理对象
    public <T> T getMapper(Class<T> type, SqlSession sqlSession) {

        return null;
    }

    public <T> boolean hasMapper(Class<T> type) {
        return knownMappers.containsKey(type);
    }

    //注册Mapper接口
    public <T> void addMapper(Class<T> type) {
        if (type.isInterface()) {
            if (hasMapper(type)) {
                throw new BindingException("Type " + type + " is already known to the MapperRegistry.");
            }

            try {
                knownMappers.put(type, new MapperProxyFactory<T>(type));

                //MapperAnnotationBuilder parser = new MapperAnnotationBuilder(config, type);
                //parser.parse();

            }finally {

            }
        }
    }
}
